package com.t.testsql;


import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.google.gson.reflect.TypeToken;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static com.t.testsql.Testprovider.CONTENT_URI_FIRST;

public class MainActivity extends AppCompatActivity {

    private Handler mHandler = new Handler();

    String json_data = "{\n" +
            "    \"code\": 100000,\n" +
            "    \"msg\": \"操作成功\",\n" +
            "    \"data\": [{\n" +
            "        \"name\": \"aa1\",\n" +
            "        \"age\": \"11\",\n" +
            "        \"phone\": \"12\"\n" +
            "    }, {\n" +
            "        \"name\": \"aa2\",\n" +
            "        \"age\": \"12\",\n" +
            "        \"phone\": \"123\"\n" +
            "    }, {\n" +
            "        \"name\": \"aa3\",\n" +
            "        \"age\": \"13\",\n" +
            "        \"phone\": \"1234\"\n" +
            "    }, {\n" +
            "        \"name\": \"aa4\",\n" +
            "        \"age\": \"14\",\n" +
            "        \"phone\": \"12345\"\n" +
            "    }, {\n" +
            "        \"name\": \"aa5\",\n" +
            "        \"age\": \"15\",\n" +
            "        \"phone\": \"123456\"\n" +
            "    }]\n" +
            "}";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       // setData();
//        queryData();
//
//        update();
//
//        queryData();

       // setData();

        //queryFromUri();

        getContentResolver().registerContentObserver(CONTENT_URI_FIRST, true,
                new MycontentObserver(mHandler));

        insertFromUri();


    }

    public void insertFromUri() {
        ArrayList<DataEntity.DataBean> enitiy = null;
        try {
            JSONObject jsonObject = new JSONObject(json_data);
            String data = jsonObject.getString("data");
            enitiy = (ArrayList<DataEntity.DataBean>) GsonUtil.parseJsonToList
                    (data, new TypeToken<List<DataEntity.DataBean>>() {
                    }.getType());
        } catch (Exception e) {
            e.printStackTrace();
        }

        ContentValues values = new ContentValues();
        for (DataEntity.DataBean bean : enitiy) {
            values.put(Sqldatabase.COULUMN_NAME, bean.getName());
            values.put(Sqldatabase.COULUMN_AGE, bean.getAge());
            values.put(Sqldatabase.COULUMN_PHONE, bean.getPhone());
//            getContentResolver().insert(CONTENT_URI_FIRST, values);
        }
        getContentResolver().insert(CONTENT_URI_FIRST, values);
    }

    public void haha(View view) {
         //setData();
        insertFromUri();
    }

    public void queryFromUri() {
        ContentResolver contentResolver = getContentResolver();
        Cursor cursor = contentResolver.query(CONTENT_URI_FIRST, null, null, null, null);
        while (cursor.moveToNext()) {
            String name = cursor.getString(cursor.getColumnIndex((Sqldatabase.COULUMN_NAME)));
            Log.e("sqlDao233", "query :" + name);
        }
    }

    public void queryData() {
        sqlDao dao = sqlDao.getInstance();
        dao.query();
       // dao.query1("aa5");
    }

    public void update() {
        sqlDao dao = sqlDao.getInstance();
        dao.update("8888","6667");
    }

    public void setData() {
        ArrayList<DataEntity.DataBean> enitiy = null;
        try {
            JSONObject jsonObject = new JSONObject(json_data);
            String data = jsonObject.getString("data");
            enitiy = (ArrayList<DataEntity.DataBean>) GsonUtil.parseJsonToList
                    (data, new TypeToken<List<DataEntity.DataBean>>() {
                    }.getType());
        } catch (Exception e) {
            e.printStackTrace();
        }
        sqlDao dao = sqlDao.getInstance();
        for (DataEntity.DataBean bean : enitiy) {
             dao.insert(bean);
        }
    }
}
